package experiment5.utils;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author:fyl
 * @date 2021/5/21 17:18
 * @Modified By:
 * @Modified Date:
 * @Description:
 */
public class C3P0Tools {
    private static ComboPooledDataSource dataSource;

    static {
        dataSource = new ComboPooledDataSource("c3p0");
    }

    /**
     * 获取连接对象
     * @return
     */
    public static Connection getConnection() {
        Connection connection = null;
        try {
            connection = dataSource.getConnection();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return connection;
    }

    /**
     * 释放资源
     * @param connection
     * @param statement
     * @param resultSet
     */
    public static void release(Connection connection, PreparedStatement statement, ResultSet resultSet){
        try {
            if (connection!=null){
                connection.close();
            }
            if (statement!=null){
                statement.close();
            }
            if (resultSet!=null){
                resultSet.close();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
